package progress.exa4;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ByteArrayOutputStreamStudy {

    public static void main(String[] args) throws IOException {
        toByteArrayStudy();
    }

    public static void toByteArrayStudy() throws IOException {
        byte[] result;
        byte[] bytes = "我是四郎".getBytes(StandardCharsets.UTF_8);
        ByteArrayOutputStream outputStream = null;
        try {
            // 新建一个字节数组输出流
            // 其内部会开辟一个大小为32的缓冲数组buf, 它作为输出流的源
            outputStream = new ByteArrayOutputStream();
            outputStream.write(bytes);
            // 这行代码将缓冲数组赋给result供程序使用
            result = outputStream.toByteArray();
            for (byte a:bytes){
                System.out.print(a);
            }
            System.out.println();
            for (byte b:result){
                System.out.print(b);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(outputStream!=null) {
                outputStream.close();
            }
        }

    }

}
